package allrobots.Robot;

import java.util.HashMap;
import java.util.concurrent.locks.ReentrantLock;

public class RobotMessageMap {

    // 存储锁
    private final static HashMap<Integer, ReentrantLock> locks = new HashMap<>();
    // 存储数据
    public final static HashMap<Integer, RobotMessage> robotMap = new HashMap<>();

    // 初始化锁
    static {
        // 为每个键值初始化锁
        locks.put(1, new ReentrantLock());
        locks.put(2, new ReentrantLock());
        // 可以根据需要继续添加其他键值的锁
    }
    // 设置键值为1的数据
    public static void setRobotMessage(int key, RobotMessage message) {
        if (locks.containsKey(key)) {
            ReentrantLock lock = locks.get(key);
            lock.lock(); // 获取锁
            try {
                robotMap.put(key, message);
            } finally {
                lock.unlock(); // 释放锁
            }
        }
    }
}
